package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.mapper.IMGroupChatUserMapper;
import com.mask.im.business.common.exception.BusinessException;
import com.mask.im.business.model.dto.GroupChatShowDTO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.entity.chat.IMGroupChatUserDO;
import com.mask.im.business.service.IMGroupChatUserService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import com.mask.im.business.common.constant.GroupChatConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 分组用户关联操作
 *
 * @author jian
 */
@Service
public class IMGroupChatUserServiceImpl extends ServiceImpl<IMGroupChatUserMapper, IMGroupChatUserDO> implements IMGroupChatUserService {

    @Override
    public void inserBatch(List<IMGroupChatUserDO> groupChatUsers) {
        this.saveBatch(groupChatUsers);
    }

    @Override
    public IMGroupChatUserDO getByUserId(Long userId, Long groupChatId) {
        return this.baseMapper.selectOne(new LambdaUpdateWrapper<IMGroupChatUserDO>()
                .eq(IMGroupChatUserDO::getGroupId, groupChatId)
                .eq(IMGroupChatUserDO::getUserId, userId));
    }

    @Override
    public RestResponse<String> leaveGroupChat(Integer groupChatId) {
        Integer userInfoId = MaskIMSecurityUtils.getUserId();
        IMGroupChatUserDO imGroupChatUser = this.baseMapper.selectOne(new LambdaQueryWrapper<IMGroupChatUserDO>()
                .eq(IMGroupChatUserDO::getUserId, userInfoId)
                .eq(IMGroupChatUserDO::getGroupId, groupChatId));
        if (Objects.isNull(imGroupChatUser) || !imGroupChatUser.getMemberStatus().equals(GroupChatConstant.ALREADY_EXISTS)) {
            throw new BusinessException("退出失败，您不在该群聊中");
        }

        imGroupChatUser.setMemberStatus(GroupChatConstant.EXITED);
        imGroupChatUser.setQuitTime(new Date());
        int updateById = this.baseMapper.updateById(imGroupChatUser);
        if (updateById > 0) {
            return RestResponse.ok("退出群聊成功");
        }
        return RestResponse.fail("退出群聊失败");
    }

    @Override
    public void updateStatus(Integer userInfoId, Integer groupChatId) {
        this.baseMapper.update(null, new LambdaUpdateWrapper<IMGroupChatUserDO>()
                .set(IMGroupChatUserDO::getMemberStatus, GroupChatConstant.EXITED)
                .eq(IMGroupChatUserDO::getGroupId, groupChatId)
                .eq(IMGroupChatUserDO::getUserId, userInfoId));
    }

    @Override
    public RestResponse<List<GroupChatShowDTO>> notGroupChatFriend(String keyword, Integer groupId) {
        Integer userInfoId = MaskIMSecurityUtils.getUserId();
        List<GroupChatShowDTO> groupChatShowDTOS = this.baseMapper.notGroupChatFriend(userInfoId, keyword);
        List<GroupChatShowDTO> groupChatShowDTOList = new ArrayList<>();
        groupChatShowDTOS.forEach(item -> {
            IMGroupChatUserDO imGroupChatUser = this.baseMapper.selectOne(new LambdaQueryWrapper<IMGroupChatUserDO>()
                    .eq(IMGroupChatUserDO::getGroupId, groupId)
                    .eq(IMGroupChatUserDO::getUserId, item.getFriendId()));
            if (Objects.isNull(imGroupChatUser)) {
                groupChatShowDTOList.add(item);
            } else {
                if (imGroupChatUser.getMemberStatus().equals(2)) {
                    groupChatShowDTOList.add(item);
                }
            }
        });
        return RestResponse.ok(groupChatShowDTOList);
    }
}

